package com.bs.service;

import com.bs.domain.AlertMemo;
import com.bs.domain.AlertMemoQuery;
import com.bs.repository.AlertMemoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wujian
 * Date: 2017-12-25
 * Time: 12:44
 */
@Service
@Transactional
public class AlertMemoService {

    @Autowired
    private AlertMemoRepository alertMemoRepository;

    public List<AlertMemoQuery> query(AlertMemo alertMemo){
        Pageable pageable = new PageRequest(alertMemo.getNum(),100, Sort.Direction.ASC,"id");
        Page<AlertMemoQuery> listPaage  =alertMemoRepository.findAlertMemoInfo(pageable);
        List<AlertMemoQuery> list = listPaage.getContent();


        List<AlertMemoQuery> conllection=list.stream()
            .filter(alertMemo.getUserNo()!=null&&!alertMemo.getUserNo().equals("")?x->x.getUserNo().contains(alertMemo.getUserNo()):x->true)
            .filter(alertMemo.getAlertName()!=null&&!alertMemo.getAlertName().equals("")?x->x.getAlertName().equals(alertMemo.getAlertName()):x->true)
            .filter(alertMemo.getModuleName()!=null&&!alertMemo.getModuleName().equals("")?x->x.getModuleName().contains(alertMemo.getModuleName()):x->true)
            .filter(alertMemo.getPartitionId()!=null&&!alertMemo.getPartitionId().equals("")?x->x.getPartitionId().equals(alertMemo.getPartitionId()): x->true)
            .filter(alertMemo.getBeginDateTime()!=null&&!alertMemo.getBeginDateTime().equals("")?x->x.getRecCreateTime().after(alertMemo.getBeginDateTime()): x->true)
            .filter(alertMemo.getEndDateTime()!=null&&!alertMemo.getEndDateTime().equals("")?x->x.getRecCreateTime().before(alertMemo.getEndDateTime()):x->true)
            .collect(Collectors.toList());
        return conllection;
    }

    public void save(AlertMemo alertMemo){
        alertMemoRepository.save(alertMemo);
    }

    public void delete(AlertMemo alertMemo){
        alertMemoRepository.delete(alertMemo);
    }

    /**
     * 条件查询时动态组装条件
     */
    private Specification<AlertMemo> where(AlertMemo alertMemo) {
        return new Specification<AlertMemo>() {
            @Override
            public Predicate toPredicate(Root<AlertMemo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<Predicate>();

                if (alertMemo.getAlertName() != null && !alertMemo.getAlertName().equals("")) {
                    predicates.add(cb.equal(root.<String>get("alertName"), alertMemo.getAlertName()));
                }
                if (alertMemo.getPartitionId() != null && !alertMemo.getPartitionId().equals("")) {
                    predicates.add(cb.equal(root.<String>get("partitionId"), alertMemo.getPartitionId()));
                }
                if (alertMemo.getModuleName() != null && !alertMemo.getModuleName().equals("")) {
                    predicates.add(cb.like(root.<String>get("moduleName"), "%" + alertMemo.getModuleName() + "%"));
                }
                if (alertMemo.getMsgType() != null && !alertMemo.getMsgType().equals("")) {
                    predicates.add(cb.equal(root.<Integer>get("msgType"), alertMemo.getMsgType()));
                }
                if (alertMemo.getUserNo() != null && !alertMemo.getUserNo().equals("")) {
                    predicates.add(cb.equal(root.<String>get("userNo"), alertMemo.getUserNo()));
                }
                if (alertMemo.getBeginDateTime() != null && !alertMemo.getBeginDateTime().equals("")
                    && alertMemo.getEndDateTime() != null && !alertMemo.getEndDateTime().equals("")) {
                    predicates.add(cb.between(root.<Timestamp>get("recCreateTime"), alertMemo.getBeginDateTime(), alertMemo.getEndDateTime()));
                }

                return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
            }
        };
    }


}
